﻿Imports System.Runtime.InteropServices
Namespace API
    ''' <summary>Console-related API declarations</summary>
    Friend Module Console
        ''' <summary>Allocates a new console for the calling process.</summary>
        ''' <returns>If the function succeeds, the return value is nonzero.</returns>
        ''' <remarks>A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console. A process can use the FreeConsole function to detach itself from its current console, then it can call AllocConsole to create a new console or AttachConsole to attach to another console.</remarks>
        Public Declare Function AllocConsole Lib "kernel32.dll" () As Boolean
        ''' <summary>Detaches the calling process from its console.</summary>
        ''' <returns>If the function succeeds, the return value is nonzero. If the function fails, the return value is zero. To get extended error information, call GetLastError.</returns>
        Public Declare Function FreeConsole Lib "kernel32.dll" () As Boolean
        ''' <summary>Sets console icon</summary>
        ''' <param name="hicon">Handle of icon (<see cref="Icon.Handle"/>)</param>
        ''' <returns>True of succes, false on error</returns>
        ''' <remarks>This function is undocumented</remarks>
        Public Declare Function SetConsoleIcon Lib "Kernel32.dll" (ByVal hicon As IntPtr) As Boolean
        ''' <summary>An application-defined function used with the SetConsoleCtrlHandler function. A console process uses this function to handle control signals received by the process. When the signal is received, the system creates a new thread in the process to execute the function.</summary>
        ''' <param name="dwCtrlType">The type of control signal received by the handler.</param>
        ''' <returns>If the function handles the control signal, it should return TRUE. If it returns FALSE, the next handler function in the list of handlers for this process is used.</returns>
        ''' <remarks>Because the system creates a new thread in the process to execute the handler function, it is possible that the handler function will be terminated by another thread in the process. Be sure to synchronize threads in the process with the thread for the handler function.</remarks>
        Public Delegate Function HandlerRoutine(ByVal dwCtrlType As ControlType) As Boolean
        ''' <summary>Close reasons for <see cref="HandlerRoutine"/></summary>
        Public Enum ControlType As Integer
            ''' <summary>A CTRL+C signal was received, either from keyboard input or from a signal generated by the GenerateConsoleCtrlEvent function.</summary>
            CTRL_C_EVENT = 0
            ''' <summary>A CTRL+BREAK signal was received, either from keyboard input or from a signal generated by GenerateConsoleCtrlEvent.</summary>
            CTRL_BREAK_EVENT = 1
            ''' <summary>A signal that the system sends to all processes attached to a console when the user closes the console (either by clicking Close on the console window's window menu, or by clicking the End Task button command from Task Manager).</summary>
            CTRL_CLOSE_EVENT = 2
            ''' <summary><para>A signal that the system sends to all console processes when a user is logging off. This signal does not indicate which user is logging off, so no assumptions can be made.</para>
            ''' <para>Note that this signal is received only by services. Interactive applications are terminated at logoff, so they are not present when the system sends this signal.</para></summary>
            CTRL_LOGOFF_EVENT = 3
            ''' <summary><para>A signal that the system sends when the system is shutting down. Interactive applications are not present by the time the system sends this signal, therefore it can be received only be services in this situation. Services also have their own notification mechanism for shutdown events. For more information, see Handler.</para>
            ''' <para>This signal can also be generated by an application using GenerateConsoleCtrlEvent.</para></summary>
            CTRL_SHUTDOWN_EVENT = 4
        End Enum
        ''' <summary><para>Adds or removes an application-defined HandlerRoutine function from the list of handler functions for the calling process.</para>
        ''' <para>If no handler function is specified, the function sets an inheritable attribute that determines whether the calling process ignores CTRL+C signals.</para></summary>
        ''' <param name="HandlerRoutine">A pointer to the application-defined HandlerRoutine function to be added or removed. This parameter can be NULL. </param>
        ''' <param name="Add"><para>If this parameter is TRUE, the handler is added; if it is FALSE, the handler is removed.</para>
        ''' <para>If the HandlerRoutine parameter is NULL, a TRUE value causes the calling process to ignore CTRL+C input, and a FALSE value restores normal processing of CTRL+C input. This attribute of ignoring or processing CTRL+C is inherited by child processes.</para></param>
        ''' <returns>If the function succeeds, the return value is nonzero.</returns>
        Public Declare Function SetConsoleCtrlHandler Lib "Kernel32.dll" (ByVal HandlerRoutine As HandlerRoutine, ByVal Add As Boolean) As Boolean
        ''' <summary>Retrieves the window handle used by the console associated with the calling process.</summary>
        ''' <returns>The return value is a handle to the window used by the console associated with the calling process or NULL if there is no such associated console.</returns>
        Public Declare Function GetConsoleWindow Lib "Kernel32.dll" () As IntPtr
    End Module
End Namespace
